package com.nbjtjc.safe.service.sys;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.sys.SysUserReportMapper;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.model.sys.SysUserReport;

@Service
public class SysUserReportService {

	@Autowired
	SysUserReportMapper sysUserReportMapper;
	
    @Transactional(rollbackFor = {Throwable.class})
	public Result addByYear(List<SysUser> sysUsers,String year){
		Result result = new Result();
		if(CollectionUtil.isEmpty(sysUsers)){
			result.setFalse("用户内容为空");
			return result;
		}
		int res = 0;
		//该年查询结果为空则新增；否则更新，删除旧的、添加新的。
		if(CollectionUtil.isEmpty(sysUserReportMapper.searchByYear(year))){//新增
			result = insertSysUserReports(sysUsers,year);

		}else{//更新
			res = sysUserReportMapper.deleteByYear(year);
			if(res==0){
				result.setFalse("新增内容失败");
				return result;
			}
			result = insertSysUserReports(sysUsers,year);
		}

		return result;
	}
    
    public Result insertSysUserReports(List<SysUser> sysUsers,String year){
    	Result result = new Result();
		List<SysUserReport> sysUserReports = new ArrayList();

		for(SysUser user:sysUsers){
			SysUserReport sysUserReport = new SysUserReport();
			sysUserReport.setYear(year);
			
			sysUserReport.setUserId(user.getUserId());
			sysUserReport.setUsername(user.getUsername());
			sysUserReport.setPassword(user.getPassword());
			sysUserReport.setSalt(user.getSalt());
			sysUserReport.setEmail(user.getEmail());
			sysUserReport.setShortPhone(user.getShortPhone());
			sysUserReport.setMobile(user.getMobile());
			sysUserReport.setStatus(user.getStatus());
			sysUserReport.setDeptId(user.getDeptId());
			sysUserReport.setCreateTime(user.getCreateTime());
			sysUserReport.setLoginName(user.getLoginName());
			sysUserReport.setDeptCode(user.getDeptCode());
			sysUserReport.setCompanyDeptCode(user.getCompanyDeptCode());
			sysUserReport.setCompanyId(user.getCompanyId());
			sysUserReport.setBirthday(user.getBirthday());
			sysUserReport.setDegreeOfEducation(user.getDegreeOfEducation());
			sysUserReport.setEntryDate(user.getEntryDate());
			sysUserReport.setDuty(user.getDuty());
			sysUserReport.setGender(user.getGender());
			sysUserReport.setTeamId(user.getTeamId());
			
			sysUserReport.setTemp(user.getTemp());
			sysUserReport.setIdNumber(user.getIdNumber());
			sysUserReport.setEntryTrain(user.getEntryTrain());
			sysUserReport.setLeaveFlag(user.getLeaveFlag());
			sysUserReport.setLeaveDate(user.getLeaveDate());
			sysUserReport.setReassignFlag(user.getReassignFlag());
			sysUserReport.setReassignDate(user.getReassignDate());
			sysUserReport.setReassignTrain(user.getReassignTrain());
			sysUserReport.setCurrentComp(user.getCurrentComp());

			sysUserReports.add(sysUserReport);
		}
    	int i=sysUserReportMapper.insertList(sysUserReports);
		if(i==0){
			result.setFalse("新增内容失败");
			return result;
		}
		result.setData(sysUserReports);
    	return result;
    }
    
    
	public Result searchByYear(String year){
		Result result = new Result();
		result.setData(sysUserReportMapper.searchByYear(year));

		return result;
	}
}
